<?php
namespace app\farm\controller;
use app\AdminRest;
use app\farm\model\AdminRole;
use app\farm\model\BalanceWater;
use app\farm\model\Evaluate;
use app\farm\model\FinanceWater;
use app\farm\model\Node;
use app\farm\model\Role;
use app\massage\controller\Admin;
use app\massage\model\CarUserTrophy;
use app\massage\model\Commission;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Date;
use app\massage\model\OrderGoods;
use app\massage\model\RefundOrder;
use app\shop\model\IntegralLog;
use app\shop\model\Wallet;
use think\App;
use app\farm\model\User as Model;


class AdminUser extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-24 10:24
     * @功能说明:用户列表
     */
    public function userList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];
        //是否授权
        if(!empty($input['type'])){

            if($input['type']==1){

                $dis[] = ['avatarUrl','=',''];

            }else{

                $dis[] = ['avatarUrl','<>',''];

            }

        }

        if(isset($input['role'])){

            $dis[] = ['role','=',$input['role']];
        }

        if(isset($input['is_black'])){

            $dis[] = ['is_black','=',$input['is_black']];
        }

        $where = [];

        if(!empty($input['nickName'])){

            $where[] = ['nickName','like','%'.$input['nickName'].'%'];

            $where[] = ['phone','like','%'.$input['nickName'].'%'];
        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['create_time','between',"$start_time,$end_time"];
        }

        $data = $this->model->dataList($dis,$input['limit'],$where);

        if(!empty($data['data'])){

            $finance_model = new FinanceWater();

            $water_model   = new BalanceWater();

            $inte_model    = new IntegralLog();

            foreach ($data['data'] as &$v){

                $cash_data = $finance_model->getUserCashData($v['id']);
                //总消费
                $v['pay_cash']    = $cash_data['cash'];
                //总退款
                $v['refund_cash'] = $cash_data['refund_cash'];
                //充值金额
                $v['balance_cash']= $water_model->where(['type'=>1,'user_id'=>$v['id']])->sum('price');
                //总积分
                $v['total_integral'] = $inte_model->where(['user_id'=>$v['id']])->where('integral_add','>',0)->sum('integral_add');
                //兑换积分
                $v['pay_integral'] = round($v['total_integral']-$v['integral'],2);


            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-26 14:39
     * @功能说明:编辑员工
     */
    public function userUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
         ];

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-08-28 23:03
     * @功能说明:佣金记录
     */
    public function commList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];


        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        if(!empty($input['top_name'])){

            $dis[] = ['c.nickName','like','%'.$input['top_name'].'%'];

        }

        $comm_model = new Commission();

        $data = $comm_model->recordList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-26 15:02
     * @功能说明:用户所有获得的奖杯
     */
    public function userTrophy(){

        $input = $this->_param;

        $user_trophy_model = new CarUserTrophy();

        $dis = [

            'a.user_id' => $input['user_id'],

            'a.status'  => 1,

            'b.status'  => 1

        ];

        $data = $user_trophy_model->alias('a')
                ->join('shequshop_car_trophy b','a.trophy_id = b.id')
                ->where($dis)
                ->field('a.*,b.title,b.cover')
                ->select()
                ->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-26 15:04
     * @功能说明:用户添加奖杯
     */
    public function userTrophyAdd(){

        $input = $this->_input;

        $user_trophy_model = new CarUserTrophy();

        foreach ($input['user_id'] as $value){

            foreach ($input['trophy_id'] as $v){

                $dis = [

                    'user_id'  => $value,

                    'trophy_id'=> $v,

                    'uniacid'  => $this->_uniacid
                ];

                $find = $user_trophy_model->where($dis)->where('status','>',-1)->find();
                //增加
                if($input['add']==1){

                     if(empty($find)){

                         $user_trophy_model->dataAdd($dis);
                     }

                }else{
                    //减少
                    if(!empty($find)){

                        $user_trophy_model->dataUpdate($dis,['status'=>-1]);
                    }

                }

            }

        }

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-10 13:32
     * @功能说明:评价管理
     */
    public function evaluateList(){

        $input = $this->_param;

        $eva_model = new Evaluate();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status','>',-1];

        if(!empty($input['type'])){

            $dis[] = ['a.type','=',$input['type']];

        }

        if(!empty($input['good'])){

            $dis[] = ['a.star','>=',3];

        }

        if(!empty($input['bad'])){

            $dis[] = ['a.star','<',3];

        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['a.farmer_id','=',$input['farmer_id']];

        }

        if(!empty($input['title'])){

            $dis[] = ['b.title','line',"%".$input['title'].'%'];

        }

        $data = $eva_model->adminList($dis,$input['limit']);

        $where[] = ['uniacid','=',$this->_uniacid];

        $where[] = ['status','>',-1];

        if(!empty($input['type'])){

            $where[] = ['type','=',$input['type']];

        }

        $data['good_eva'] = $eva_model->where($where)->where('star','>=',3)->count();

        $data['bad_eva']  = $eva_model->where($where)->where('star','<',3)->count();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-02-10 13:45
     * @功能说明:编辑评论
     */
    public function evaluateUpdate(){

        $input = $this->_input;

        $eva_model = new Evaluate();

        $dis = [

            'id' => $input['id']
        ];

        $data = $eva_model->dataInfo($dis);

        $res = $eva_model->dataUpdate($dis,$input);

        if(!empty($input['status'])&&$input['status']==-1){
            //修改农场主的评分
            $res = $eva_model->updateFarmerStar($data['farmer_id']);
        }
        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:02
     * @功能说明:角色列表
     */
    public function roleList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $role_model = new Role();

        $data = $role_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:02
     * @功能说明:角色列表
     */
    public function roleSelect(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','=',1];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $role_model = new Role();

        $data = $role_model->where($dis)->select()->toArray();

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2022-01-04 13:56
     * @功能说明:添加角色
     */
    public function roleAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $role_model = new Role();

        $data = $role_model->dataAdd($input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:17
     * @功能说明:编辑角色
     */
    public function roleUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']

        ];

        $input['uniacid'] = $this->_uniacid;

        $role_model = new Role();
        //删除
        if(isset($input['status'])&&$input['status']==-1){

            $adminRole_mdoel = new AdminRole();

            $find = $adminRole_mdoel->alias('a')
                ->join('lbfarm_school_admin b','a.admin_id = b.id')
                ->where(['a.role_id'=>$input['id']])
                ->where('b.status','>',-1)
                ->find();

            if(!empty($find)){

                $this->errorMsg('该角色正在被使用');
            }

        }

        $data = $role_model->dataUpdate($dis,$input);

        return $this->success($data);


    }

    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:17
     * @功能说明:角色详情
     */
    public function roleInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']

        ];

        $role_model = new Role();

        $data = $role_model->dataInfo($dis);

        $node_model = new Node();

        $node = $node_model->where(['role_id'=>$input['id']])->select()->toArray();

        if(!empty($node)){

            foreach ($node as $k=>$v){

                $node[$k]['auth'] = !empty($v['auth'])?explode(',',$v['auth']):[];

            }

        }

        $data['node'] = $node;

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:18
     * @功能说明:给账号分配角色（多选）
     */
    public function adminRoleAdd(){

        $input = $this->_input;

        $adminRole_mdoel = new AdminRole();

        $adminRole_mdoel->where(['admin_id'=>$input['admin_id']])->delete();

        if(!empty($input['role'])){

            foreach ($input['role'] as $key => $value){

                $insert[$key] = [

                    'uniacid' => $this->_uniacid,

                    'admin_id'=> $input['admin_id'],

                    'role_id' => $value

                ];

            }

            $adminRole_mdoel->saveAll($insert);
        }

        return $this->success(true);

    }

    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:24
     * @功能说明:账号所匹配的角色详情
     */
    public function adminInfo(){

        $input = $this->_param;

        $adminRole_mdoel = new AdminRole();

        $admin_model = new \app\farm\model\Admin();

        $dis= [

            'id' => $input['id']
        ];

        $data = $admin_model->dataInfo($dis);

        $dis = [

            'a.uniacid' => $this->_uniacid,

            'b.status'  => 1,

            'a.admin_id'=> $input['id']
        ];

        $data['role'] = $adminRole_mdoel->alias('a')
            ->join('lbfarm_role b','a.role_id = b.id')
            ->where($dis)
            ->field('b.*,a.role_id')
            ->group('b.id')
            ->select()
            ->toArray();

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:24
     * @功能说明:账号所匹配的角色的节点详情
     */
    public function adminNodeInfo(){

        $input = $this->_param;

        $adminRole_mdoel = new AdminRole();

        $data['is_admin'] = isset($this->_user['is_admin'])?$this->_user['is_admin']:1;

        $dis = [

            'a.uniacid' => $this->_uniacid,

            'b.status'  => 1,

            'a.admin_id'=> $this->_user['id']
        ];

        $data['node'] = $adminRole_mdoel->alias('a')
            ->join('lbfarm_role b','a.role_id = b.id')
            ->join('lbfarm_node c','c.role_id = b.id')
            ->where($dis)
            ->field('c.*')
            ->group('c.id')
            ->select()
            ->toArray();

        if(!empty($data['node'])){

            foreach ($data['node'] as $k=>$v){

                $data['node'][$k]['auth'] = !empty($v['auth'])?explode(',',$v['auth']):[];

            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:41
     * @功能说明:账号列表
     */
    public function adminList(){

        $input = $this->_param;

        $admin_model = new \app\farm\model\Admin();

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        if(!empty($input['title'])){

            $dis[] = ['username','like','%'.$input['title'].'%'];

        }

        $data = $admin_model->dataList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:47
     * @功能说明:添加账号
     */
    public function adminAdd(){

        $input = $this->_input;

        $admin_model = new \app\farm\model\Admin();

        $dis = [

            'uniacid' => $this->_uniacid,

            'username'=> $input['username'],
        ];

        $find = $admin_model->where($dis)->where('status','>',-1)->find();

        if(!empty($find)){

            $this->errorMsg('账号名不能重复');
        }

        $insert = [

            'uniacid' => $this->_uniacid,

            'username'=> $input['username'],

            'o_passwd'=> $input['passwd'],

            'passwd'  => checkPass($input['passwd']),

            'is_admin'=> 0,

        ];

        $admin_model->dataAdd($insert);

        $admin_id = $admin_model->getLastInsID();

        $adminRole_mdoel = new AdminRole();

        $adminRole_mdoel->where(['admin_id'=>$admin_id])->delete();

        if(!empty($input['role'])){

            foreach ($input['role'] as $key => $value){

                $inserts[$key] = [

                    'uniacid' => $this->_uniacid,

                    'admin_id'=> $admin_id,

                    'role_id' => $value

                ];

            }

            $adminRole_mdoel->saveAll($inserts);
        }

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-04 14:47
     * @功能说明:添加账号
     */
    public function adminUpdate(){

        $input = $this->_input;

        $admin_model = new \app\farm\model\Admin();

        if(!empty($input['username'])){

            $dis = [

                'uniacid' => $this->_uniacid,

                'username'=> $input['username'],
            ];

            $find = $admin_model->where($dis)->where('status','>',-1)->where('id','<>',$input['id'])->find();

            if(!empty($find)){

                $this->errorMsg('账号名不能重复');
            }

        }

        if(!empty($input['passwd'])){

            $input['o_passwd'] = $input['passwd'];

            $input['passwd']   = checkPass($input['passwd']);

        }

        $dis = [

            'id' => $input['id']
        ];

        if(isset($input['role'])){

            $role = $input['role'];

            unset($input['role']);
        }

        $admin_model->dataUpdate($dis,$input);

        $adminRole_mdoel = new AdminRole();

        $adminRole_mdoel->where(['admin_id'=>$input['id']])->delete();

        if(!empty($role)){

            foreach ($role as $key => $value){

                $inserts[$key] = [

                    'uniacid' => $this->_uniacid,

                    'admin_id'=> $input['id'],

                    'role_id' => $value

                ];

            }

            $adminRole_mdoel->saveAll($inserts);
        }

        return $this->success(true);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-07-20 16:00
     * @功能说明:修改用户积分
     */
    public function userIntegralUpdate(){

        $input = $this->_input;

        $log_model = new IntegralLog();

        $type = $input['integral']>0?12:13;

        $res = $log_model->integralUserAdd($input['user_id'],$input['integral'],$this->_uniacid,2,$type);

        return $this->success($res);

    }







}
